package com.chens.modules.storage.controller;

import com.chens.modules.storage.model.File;
import com.chens.payload.ApiResponse;
import com.chens.modules.storage.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/storage/files")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public ResponseEntity<File> uploadFile(@RequestParam("file") MultipartFile file) {
        File uploadedFile = fileService.storeFile(file);
        return new ResponseEntity<>(uploadedFile, HttpStatus.CREATED);
    }

    @GetMapping
    public ApiResponse<Page<File>> getFiles(@RequestParam(defaultValue = "0") int page,
                                            @RequestParam(defaultValue = "10") int size) {
        Page<File> files = fileService.getFiles(page, size);
        return ApiResponse.success(files);
    }

    @GetMapping("/all")
    public ApiResponse<List<File>> getAllFiles() {
        List<File> files = fileService.getAllFiles();
        return ApiResponse.success(files);
    }

    @GetMapping("/{id}")
    public ApiResponse<File> getFileById(@PathVariable Long id) {
        File file = fileService.getFile(id);
        return ApiResponse.success(file);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteFile(@PathVariable Long id) {
        fileService.deleteFile(id);
        return ApiResponse.success(null);
    }


}